var database = require('../../data/data.js')
const app = getApp()
Page({
  data: {
    none: "none"
  },
  onShareAppMessage: function (res) {
    return {
      title: '企业趣味运动拓展',
      imageUrl: '/image/share.jpg',
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success',
        })
      },
    }
  },
  onLoad: function () {
    var that = this;
    database.count = 0;
    database.all = 0;
    wx.showLoading({
      title: '资源加载中',
    })
    wx.login({
      success(res) {
        database.code = res.code;
        wx.getSetting({
          success(res) {
            if (!res.authSetting['scope.userInfo']) {
              wx.authorize({
                scope: 'scope.userInfo',
              })
            }
          }
        })
        wx.getSetting({
          success(res) {
            if (res.authSetting['scope.userInfo']) {
              wx.getUserInfo({
                lang: "zh_CN ",
                success(e) {
                  var data = e.userInfo;
                  wx.request({
                    method: "post",                    
                    url: database.url + '/project/userAdd.action?code=' + database.code + '&avatarUrl=' + data.avatarUrl + '&city=' + data.city + '&province=' + data.province + "&country=" + data.country + '&gender=' + data.gender + '&nickname=' + data.nickName,
                  })
                }
              });
            }
          }
        })
      }
    })
    wx.request({
      url: database.url + '/project/figureAll.action',
      success(res) {
        for (var i = 0; i < res.data.length; i++) {
          wx.setStorageSync(res.data[i].id, res.data[i].view)
        }
        var data = res.data
        database.all += res.data.length;
        that.setData({
          swiper: res.data
        })
      }
    })
    wx.request({
      url: database.url + '/project/interestingAll.action',
      success(res) {
        for (var i = 0; i < res.data.length; i++) {
          wx.setStorageSync(res.data[i].id, res.data[i].view)
        }
        database.all += res.data.length;
        that.setData({
          content: res.data
        })
      }
    })
  },
  fun_detail:function(event){
    var id = event.currentTarget.dataset.id;
    var view = wx.getStorageSync(id);
    wx.setStorage({
      key: id,
      data: view + 1,
    })
    database.view = view + 1;
    wx.request({
      url: database.url + '/project/viewAdd.action?id=' +id,
    })
    wx.navigateTo({
      url: "./fun-detail/fun-detail?id=" + id
    })
  },

  detail: function (event) {
    var id = event.currentTarget.dataset.id;
    var view = wx.getStorageSync(id);
    wx.setStorage({
      key: id,
      data: view + 1,
    })
    database.view = view + 1;
    wx.request({
      url: database.url + '/project/viewAdd.action?id=' + id,
    })
    wx.navigateTo({
      url: "../detail/detail?id=" + id
    })
  },
  video: function (event) {
    var id = event.currentTarget.dataset.id;
    var view = wx.getStorageSync(id);
    wx.setStorage({
      key: id,
      data: view + 1,
    })
    database.view = view + 1;
    wx.request({
      url: database.url + '/project/viewAdd.action?id=' + id,
    })
    wx.navigateTo({
      url: "../video/video-detail/video-detail?id=" + id
    })
  },
  call: function () {
    if (database.tel == "") {
      wx.request({
        url: database.url + '/project/contactAll.action',
        success(res) {
          database.tel = res.data[0].tel;
        }
      })
    }
    if (database.tel != "") {
      wx.showActionSheet({
        itemList: [database.tel, "拨打电话"],
        success: function (res) {
          wx.makePhoneCall({
            phoneNumber: database.tel,
          })
        },
      })
    }
  },
  imageOnLoad(ev) {
    database.count++;
    if (database.count == database.all) {
      this.setData({
        none: ""
      })
      wx.hideLoading();
    }
  },
})